﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Input;
using Microsoft.Phone.Tasks;
using MobiSms.Library;
using MobiSms.Library.Attributes;
using MobiSms.Library.Helpers;
using System.Windows;
using System.Net;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using MobiSms.DataModel;
using System.Windows.Resources;
using System.IO;

namespace MobiSms.ViewModel
{
    public class AboutAndHelpViewModel:ViewModelBase
    {
#if DESIGN
#else
        Assembly assembly = Assembly.Load("MobiSms.Wasun");
#endif
        WebClient client = new WebClient();

        public AboutAndHelpViewModel()
        {
            client.DownloadStringCompleted -= client_DownloadStringCompleted;
            client.DownloadStringCompleted += client_DownloadStringCompleted;

            client.DownloadStringAsync(new Uri(string.Format("{0}{1}.xml", ConstKeys.HelpUrl, GlobalSettings.Language), UriKind.RelativeOrAbsolute));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XDocument document = XDocument.Parse(e.Result);
                var items = (from element in document.Descendants("item")
                             select new HelpInfo()
                             {
                                 Id = Convert.ToInt32(element.Element("id").Value),
                                 Question = element.Element("question").Value,
                                 Answer = element.Element("answer").Value
                             });
                HelpCollection = new ObservableCollection<HelpInfo>(items);

            }
        }

        private ObservableCollection<HelpInfo> helpCollecion;
        public ObservableCollection<HelpInfo> HelpCollection
        {
            get { return helpCollecion; }
            set
            {
                helpCollecion = value;
                RaisePropertyChanged(() => HelpCollection);
            }
        }

        public string IntroduceString
        {
            get
            {
                string file = string.Format("MobiSms.Wasun;Component/Files/intr_{0}.txt", GlobalSettings.Language);
                StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri(file, UriKind.RelativeOrAbsolute));

                string content = new StreamReader(streamInfo.Stream).ReadToEnd();

                return content;
            }
        }

        public string Version
        {
            get
            {
                return PhoneHelper.GetAppAttribute("Version");
            }
        }

        public DateTime ReleaseDate
        {
            get
            {
                var date = assembly.GetCustomAttributes(typeof(AssemblyReleaseDateAttribute), false).Cast<AssemblyReleaseDateAttribute>();
                return DateTime.Parse(date.FirstOrDefault().Value);
            }
        }

        public ICommand SendMailCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        EmailComposeTask task = new EmailComposeTask();
                        task.To = AuthorInfo.Email;
                        task.Show();
                    }
                };
            }
        }

        public ICommand OpenRateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        MarketplaceReviewTask task = new MarketplaceReviewTask();
                        task.Show();
                    }
                };
            }
        }
    }
}
